#include "main.h"

static char buf[MAX_BUF_LEN];

using namespace std;


int tcp_send_test()
{
    struct sockaddr_in servaddr;
    int sockfd;
    int i_ret = 0;

    memset(buf, 0xAB, sizeof(buf));

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    inet_pton(AF_INET, SERVER_IP, &servaddr.sin_addr.s_addr);
    servaddr.sin_port = htons(SERVER_PORT);
    i_ret = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    if (i_ret != 0) {
        return 0;
    }
    MakeSocketNonBlocking(sockfd);

    int i_send_len = 0;
    struct timeval st_init_time;
    INT64 ll_sys_ms_time;
    sys_time_getms_r(st_init_time, st_init_time, &ll_sys_ms_time);
    for (;;) {
        int i_ret = send(sockfd, buf, sizeof(buf), 0);
        if (i_ret >= 0)  {
            i_send_len += i_ret;
//            printf("Now send %d KB\n", i_ret/ONE_KB);
//            printf("Now send %d MB\n", i_ret/ONE_MB);
//            printf("Has send %d GB\n", i_send_len/ONE_GB);
        } else {
              //printf("err[%d]=%s\n", errno, strerror(errno));
//            printf("Has send %d GB\n", i_send_len/ONE_GB);
//            printf("\r\n");
        }
        struct timeval st_now_time;
        sys_time_getms_r(st_init_time, st_now_time, &ll_sys_ms_time);
        if (ll_sys_ms_time > 1000) {
            float f_sec = (ll_sys_ms_time/1000);
            if (i_send_len/ONE_MB) {
                printf("Now %0.3f sec send %0.3f MB\n", f_sec, (float)i_send_len/ONE_MB/f_sec);
            } else {
                if (i_send_len/ONE_KB) { printf("Now %0.3f sec send %0.3f KB\n", (float)i_send_len/ONE_KB/f_sec ); }
            }
            i_send_len = 0;
            st_init_time = st_now_time;
        }

        //ms_msleep(1);
    }

    close(sockfd);

    return 0;
}


